package com.example.movie.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.MovieVideoEntity
import com.example.image.ImgOptions
import com.example.image.LoadImageUtil
import com.example.movie.databinding.ItemMovieLayoutBinding


/**
 * Author: Jason Zhang
 * Date: 2025/2/10
 * Desc: Paging数据适配器对象
 */
class MoviePagingDataAdapter: PagingDataAdapter<MovieVideoEntity, MoviePagingDataAdapter.MovieViewHolder>(MovieEntityComparator){

    private var itemClickListener: MovieItemClickListener? = null

    fun setItemClickListener(listener: MovieItemClickListener){
        itemClickListener = listener
    }

    inner class MovieViewHolder(val dataBinding: ItemMovieLayoutBinding): RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
        return MovieViewHolder(ItemMovieLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemViewType(position: Int): Int = 0

    override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
        val entity = getItem(position)
        if (entity != null){
            LoadImageUtil.loadUrl(
                holder.dataBinding.root.context,
                entity.videomainimag,
                holder.dataBinding.ivMovieImg,
                ImgOptions.Builder.isCircle(false).build()
            )

            holder.dataBinding.apply {
                tvMovieTitle.text = entity.name
                tvMovieDesc.text = entity.description
                tvMovieTag.text = entity.taglist
            }

            //设置点击事件
            holder.dataBinding.root.setOnClickListener {
                itemClickListener?.onItemChildClick(it, position, entity)
            }
        }
    }

    interface MovieItemClickListener{
        fun onItemChildClick(v: View, position: Int, entity: MovieVideoEntity)
    }


}